/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author mwh
 */
public class Grep {

    public static List<String> grep(String regExp, String filename) throws FileNotFoundException, IOException {

        List<String> res = new ArrayList<String>();
        String re = ".*" + regExp + ".*";
        BufferedReader input = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = input.readLine()) != null) {

            if (line.matches(re)) {
                res.add(line);
            }
        }
        return res;
    }


    public static String grepFirst(String regExp, String filename) throws FileNotFoundException, IOException {


        String re = ".*" + regExp + ".*";
        BufferedReader input = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = input.readLine()) != null) {

            if (line.matches(re)) {
                return line;
            }
        }

        return "";
    }
}
